<?php
/**
   {pageNavigation 
		count=countOfAll 
		[skip=0] 
		[limit=20] 
		[numOfPagesVisible=5] 
		[link=$_SERVER['REQUEST_URI']] 
		[showTitle=true]
		[showFirstLast=true]
		[showPrevNext=true]
	}
*/

function smarty_function_pageNavigation($params)
{
   global $smarty;

   $count = $params['count'];
   $skip = array_key_exists('skip',$params) ? $params['skip'] : 0;
   $limit = array_key_exists('limit',$params) ? $params['limit'] : 20;
   $numOfPagesVisible = array_key_exists('numOfPagesVisible',$params) ? $params['numOfPagesVisible'] : 5;

   $pagesCount = ceil($count/$limit);
   $currPage = ceil($skip/$limit) + 1;

   $pagesBefore = floor(($numOfPagesVisible-1)/2);
   $pagesAfter = ceil(($numOfPagesVisible-1)/2);

   if (($pagesCount-$currPage) < $pagesAfter) 
   {
      $pagesBefore += ($pagesAfter - ($pagesCount - $currPage));
   }
   
   $pageNum = $currPage - $pagesBefore;
   if ($pageNum <= 0) {
      $pageNum = 1;
   }

   $pageNavigation = array();
   for ($i=0; $i<$numOfPagesVisible && $pageNum<=$pagesCount; ++$i,++$pageNum)
   {
      $pageNavigation[] = $pageNum;
   }
   
   $link = $_SERVER['REQUEST_URI'];
   if (array_key_exists('link',$params))
   {
      $link = $params['link'];
   }
   
   $showTitle = true;
   if (array_key_exists('showTitle',$params))
   {
      $showTitle = $params['showTitle'];
   }

   $showFirstLast = true;
   if (array_key_exists('showFirstLast',$params))
   {
      $showFirstLast = $params['showFirstLast'];
   }

   $showPrevNext = true;
   if (array_key_exists('showPrevNext',$params))
   {
      $showPrevNext = $params['showPrevNext'];
   }

   $smarty->assign('skip', $skip);
   $smarty->assign('limit', $limit);
   $smarty->assign('count', $count);
   $smarty->assign('link', $link);
   $smarty->assign('showTitle', $showTitle);
   $smarty->assign('showFirstLast', $showFirstLast);
   $smarty->assign('showPrevNext', $showPrevNext);
   $smarty->assign('currPage', $currPage);
   $smarty->assign('pagesCount', $pagesCount);
   $smarty->assign('pageNavigation', $pageNavigation);
   
   return $smarty->fetch("cf/templates/pageNavigation.tpl");
}

?>